/**
 * 
 */
package com.huawei.esdk.uc.sametime.services;

import java.util.Map;

import org.apache.log4j.Logger;
import org.eclipse.jface.preference.IPreferenceStore;

import com.huawei.esdk.uc.jna.Constants;
import com.huawei.esdk.uc.jna.UCService;
import com.huawei.esdk.uc.jna.Constants.PhoneJointType;
import com.huawei.esdk.uc.jna.Constants.UCServiceRetvCode;
import com.huawei.esdk.uc.jna.mode.PhoneJointDevTypeRes;
import com.huawei.esdk.uc.jna.struct.STContact;
import com.huawei.esdk.uc.jna.struct.STDeviceListParam;
import com.huawei.esdk.uc.jna.struct.STDeviceParam;
import com.huawei.esdk.uc.jna.util.StringUtil;
import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.EsdkPluginActivator;
import com.huawei.esdk.uc.sametime.preferences.PreferenceConstants;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;

/**
 * 首选项中设置相关的控制类
 * @author y00175154
 * 
 */
public class UCSettingsService
{

    private static final Logger logger = Logger
            .getLogger(UCSettingsService.class);

    /**
     * 获取麦克风设备列表
     * @return 麦克风设备列表的二维数组,里面的每一项都是{设备名字,设备编号}
     */
    public static String[][] getMircophoneDevList()
    {
        logger.debug("getMircophoneDevList");
        // 这里调用UC SDK的麦克风设备类型获取接口
        // 将获取的设备类型数据存储在mircohpneDev字符串数组对象中
        STDeviceListParam deviceListParam = UCService.getInstance()
                .UC_SDK_GetMicDevList(0, 4);
        String[][] mircohpneDev = null;
        if (null != deviceListParam)
        {
            mircohpneDev = new String[deviceListParam.iTotal][2];
            STDeviceParam[] params = deviceListParam.deviceList;

            for (int i = 0; i < deviceListParam.iTotal; i++)
            {
                mircohpneDev[i][0] = StringUtil.bytesToString(params[i].name);
                mircohpneDev[i][1] = String.valueOf(params[i].index);
            }
        }

        if (null == mircohpneDev)
        {
            mircohpneDev = new String[0][0];
        }

        logger.debug("getMircophoneDevList | mircohpneDev.length = "
                + mircohpneDev.length);

        return mircohpneDev;
    }

    /**
     * 获取扬声器设备列表
     * @return 扬声器设备列表的二维数组,里面的每一项都是{设备名字,设备编号}
     */
    public static String[][] getSpeakerDevList()
    {
        // 这里调用UC SDK的扬声器设备类型获取接口
        // 将获取的设备类型数据存储在speakerDev字符串数组对象中
        STDeviceListParam deviceListParam = UCService.getInstance()
                .UC_SDK_GetSpeakerDevList(0, 4);
        String[][] speakerDev = null;
        if (null != deviceListParam)
        {
            speakerDev = new String[deviceListParam.iTotal][2];
            STDeviceParam[] params = deviceListParam.deviceList;

            for (int i = 0; i < deviceListParam.iTotal; i++)
            {
                speakerDev[i][0] = StringUtil.bytesToString(params[i].name);
                speakerDev[i][1] = String.valueOf(params[i].index);
            }
        }
        if (null == speakerDev)
        {
            speakerDev = new String[0][0];
        }

        logger.debug("getSpeakerDevList | speakerDev.length = "
                + speakerDev.length);

        return speakerDev;
    }

    /**
     * 获取视频设备(摄像头)列表
     * @return 视频设备设备列表的二维数组,里面的每一项都是{设备名字,设备编号}
     */
    public static String[][] getCameraDevList()
    {
        // 这里调用UC SDK的摄像头设备类型获取接口
        // 将获取的设备类型数据存储在cameraDev字符串数组对象中
        STDeviceListParam deviceListParam = UCService.getInstance()
                .UC_SDK_GetVideoDevList(0, 4);
        String[][] cameraDev = null;
        if (null != deviceListParam)
        {
            cameraDev = new String[deviceListParam.iTotal][2];
            STDeviceParam[] params = deviceListParam.deviceList;

            for (int i = 0; i < deviceListParam.iTotal; i++)
            {
                cameraDev[i][0] = StringUtil.bytesToString(params[i].name);
                cameraDev[i][1] = String.valueOf(params[i].index);

                logger.debug("name = "
                        + StringUtil.bytesToString(params[i].name)
                        + ", index = " + params[i].index);
            }
        }

        if (null == cameraDev)
        {
            cameraDev = new String[0][0];
        }

        logger.debug("getCameraDevList | cameraDev.length = "
                + cameraDev.length);

        return cameraDev;
    }

    public static boolean setMircophoneDev(int index)
    {
        // 这里调用UC SDK的修改麦克风设备类型接口
        // 修改成功，返回true，修改失败，返回false
        int ret = UCService.getInstance().UC_SDK_SetCurrentMicDev(index);
        if (UCServiceRetvCode.UC_SDK_Success == ret)
        {
            return true;
        }
        return false;
    }

    public static boolean setSpeakerDev(int index)
    {
        // 这里调用UC SDK的修改扬声器设备类型接口
        // 修改成功，返回true，修改失败，返回false
        int ret = UCService.getInstance().UC_SDK_SetCurrentSpeakerDev(index);
        if (UCServiceRetvCode.UC_SDK_Success == ret)
        {
            return true;
        }
        return false;
    }

    public static boolean setCameraDev(int index)
    {
        // 这里调用UC SDK的修改摄像头设备类型接口
        // 修改成功，返回true，修改失败，返回false
        int ret = UCService.getInstance().UC_SDK_SetCurrentVideoDev(index);
        if (UCServiceRetvCode.UC_SDK_Success == ret)
        {
            return true;
        }
        return false;
    }

    public static boolean isIPPhoneEnabled()
    {
        // 这里调用UC SDK的IP Phone是否激活可用状态的接口
        return true;
    }

    public static boolean setAnswerMode(String type, String[] numbers)
    {
        // 这里调用UC SDK的设置同振、顺振的服务接口
        // type = "0"：同振 ; type = "1"：顺振
        return true;
    }

    public static boolean setDefaultOutgoingCallDev(String type)
    {
        logger.debug("UCSettingsService | setDefaultOutgoingCallDev | type = "
                + type);
        // 这里调用UC SDK的修改默认呼出设备类型接口
        // 修改成功，返回true，修改失败，返回false
        int iRet = -1;
        if (type.equals("softphone"))
        {
            iRet = UCService.getInstance().UC_SDK_SetPhoneJointDevType(
                    PhoneJointType.PC_Device);
        }
        else
        {
            iRet = UCService.getInstance().UC_SDK_SetPhoneJointDevType(
                    PhoneJointType.IPPhone_Device);
        }
        return 0 == iRet;
    }

    public static String getPhoneJoinDev()
    {
        PhoneJointDevTypeRes phoneJointDevTypeRes = UCService.getInstance()
                .UC_SDK_GetPhoneJointDevType();
        int devType = phoneJointDevTypeRes.getDevType();
        int retCode = phoneJointDevTypeRes.getRetCode();

        logger.info("get PhoneJoint Device with retcode: " + retCode
                + ", devType: " + devType);

        if (0 != retCode)
        {
            logger.error("get PhoneJoint Device failed with retcode: "
                    + retCode);

            return ConstantsDefines.UNKNOWN_MODE;
        }
        GlobalConfiguration.getInstance().setCallDevice(devType);

        if (Constants.PhoneJointType.PC_Device == devType)
        {
            return ConstantsDefines.PC_MODE;
        }
        else if (Constants.PhoneJointType.IPPhone_Device == devType)
        {
            return ConstantsDefines.IP_PHONE_MODE;
        }
        else
        {
            return ConstantsDefines.PC_MODE;
        }
    }

    public static void setDevices()
    {
        IPreferenceStore preferenceStore = EsdkPluginActivator.getDefault()
                .getPreferenceStore();
        String cameraType = preferenceStore
                .getString(PreferenceConstants.P_CAMERA);
        String microphoneType = preferenceStore
                .getString(PreferenceConstants.P_MIRCOPHONE);
        String speakerType = preferenceStore
                .getString(PreferenceConstants.P_SPEAKER);

        String[][] cameras = UCSettingsService.getCameraDevList();
        if (cameras.length > 0)
        {
            UCSettingsService.setCameraDev(StringUtil.getValueForName(cameras,
                    cameraType));
        }

        String[][] microphone = UCSettingsService.getMircophoneDevList();
        if (microphone.length > 0)
        {
            UCSettingsService.setMircophoneDev(StringUtil.getValueForName(
                    microphone, microphoneType));
        }

        String[][] speaker = UCSettingsService.getSpeakerDevList();
        if (speaker.length > 0)
        {
            UCSettingsService.setSpeakerDev(StringUtil.getValueForName(speaker,
                    speakerType));
        }
    }

    /**
     * 获取当前登录UC账号的IP Phone号码
     * @return
     */
    public static String getSelfIPPhoneNum()
    {
        String iphone = null;
        Map<String, String> currentUCInfo = GlobalConfiguration.getInstance()
                .getSelfUCAccountInfo();
        iphone = currentUCInfo
                .get(ConstantsDefines.UC_CONTACT_PROPERTIES.iphone1);
        if (null == iphone)
        {
            String currentUCAccount = currentUCInfo
                    .get(ConstantsDefines.UC_CONTACT_PROPERTIES.account);
            STContact contact = UCService.getInstance().UC_SDK_GetContactInfo(
                    currentUCAccount);
            if (null != contact)
            {
                iphone = StringUtil.bytesToUTF8String(contact.ipphone1);
                currentUCInfo.put(
                        ConstantsDefines.UC_CONTACT_PROPERTIES.iphone1, iphone);
            }
        }

        return iphone;
    }

}
